/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     http://Yi.tech
 * @date:        2019/5/20 20:57
 * @email:       https:yiyuery.github.io/NoteBooks
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.swagger.common.base;

import com.example.swagger.api.ITransformPlugin;
import com.example.swagger.common.configuration.ApplicationYamlLoader;
import com.example.swagger.common.configuration.PropertiesConstant;
import com.example.swagger.common.configuration.SwaggerEnhanceConfig;
import com.example.swagger.common.context.AppBeanContext;
import com.example.swagger.common.enums.SwaggerResourceEnum;
import com.example.swagger.common.enums.SwaggerSupportEnum;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-06-05 22:59
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-06-05
 * @modify reason: {方法名}:{原因}
 * ...
 */
@Slf4j
public abstract class AbstractSwaggerTransformPlugin implements ITransformPlugin<Swagger2MarkupConfig> {

    private SwaggerEnhanceConfig swaggerEnhanceConfig = AppBeanContext.getInstance().getBean("swaggerEnhanceConfig", SwaggerEnhanceConfig.class);

    /**
     * 生成函数
     */
    public abstract void gen();

    /**
     * 具体实现由子类完成
     *
     * @throws Exception
     */
    public void apiDocs() {

    }

    @Override
    public void transform(Swagger2MarkupConfig config) {

    }

    @Override
    public void transform() {

    }

    /**
     * 转换
     *
     * @param config
     */
    @Override
    public void transform(Swagger2MarkupConfig config, SwaggerSupportEnum type) {
        try {

            String resourceOpt = ApplicationYamlLoader.getPropsByKey(PropertiesConstant.EHANCE_CONFIG_API_DOCS_SWAGGER_RESOURCE_TYPE_KEY, String.class);
            SwaggerResourceEnum resourceEnum = Objects.requireNonNull(SwaggerResourceEnum.valueOfType(resourceOpt));
            Swagger2MarkupConverter converter = null;
            //读取Swagger Resources
            if (resourceEnum.equals(SwaggerResourceEnum.SWAGGER_RESOURCE_URL)) {
                converter = Swagger2MarkupConverter.from(new URL(swaggerEnhanceConfig.getApiDocsUrl()))
                        .withConfig(config)
                        .build();
            } else {
                converter = Swagger2MarkupConverter.from(Paths.get(swaggerEnhanceConfig.getJsonPath()))
                        .withConfig(config)
                        .build();
            }
            //是否合并
            if (isCombine(type)) {
                converter.toFile(Paths.get(ApplicationYamlLoader.getPropsByKey(
                        String.format(PropertiesConstant.EHANCE_CONFIG_COMBINE_GENERATOR_KEY, type.getKey()),
                        String.class),swaggerEnhanceConfig.getName()));
            } else {
                converter.toFolder(Paths.get(ApplicationYamlLoader.getPropsByKey(
                        String.format(PropertiesConstant.EHANCE_CONFIG_GENERATOR_KEY, type.getKey()),
                        String.class),swaggerEnhanceConfig.getName()));
            }
        } catch (Exception e) {
            log.error("transform occur error!", e);
        }
    }

    protected Boolean isCombine(SwaggerSupportEnum type) {
        return ApplicationYamlLoader.getPropsByKey(
                String.format(PropertiesConstant.EHANCE_CONFIG_COMBINE_KEY, type.getKey()),
                Boolean.class);
    }

}
